//
//  ViewController.swift
//  nav 侧滑
//
//  Created by apple on 16/2/23.
//  Copyright © 2016年 machao. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    let button: UIButton = {
        let button = UIButton()
        button.setTitle("button", forState: .Normal)
        button.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        button.backgroundColor = UIColor.redColor()
        button.center = CGPoint(x: -100, y: 200)
        return button
    }()
    var navVC: UINavigationController?
    var navView : UIView?
    var animator: UIDynamicAnimator?
    var snap: UISnapBehavior?
    override func viewDidLoad() {
        super.viewDidLoad()
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.orangeColor()
        let nav = UINavigationController(rootViewController: vc)

        vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "哈哈", style: .Plain, target: self, action: "btnClick")
        vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "嘿嘿", style: .Plain, target: self, action: "pushView")
        nav.navigationBar.setBackgroundImage(UIImage(), forBarMetrics:.Default)
        nav.navigationBar.shadowImage = UIImage();
        navVC = nav
        navView = nav.view
        self.view.addSubview(nav.view)
    }

    override func loadView() {
        super.loadView()
        
    }
    override func viewWillAppear(animated: Bool) {
        navigationController?.navigationBar.translucent = true;
        navigationController?.navigationBar.alpha = 0;
    }
    func btnClick(){
        print("哈哈点击了")

        if self.navView?.frame.origin.x > 0 {
            
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.navView?.frame = CGRect(x:0, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: UIScreen.mainScreen().bounds.size.height)
                self.button.center = CGPoint(x: -100, y: 200) 
                }, completion: { (_) -> Void in
                    self.button.removeFromSuperview()
            })
            
            
        }else{
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.navView?.frame = CGRect(x: 200, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: UIScreen.mainScreen().bounds.size.height)

            }, completion: { (_) -> Void in
                self.view.addSubview(self.button)
             self.attachanimoter()
        })
        
        }
    }
    
    func pushView(){
        let vc1 = UIViewController()
        vc1.view.backgroundColor = UIColor.yellowColor()
        self.navVC?.pushViewController(vc1, animated: true)
    }
  
    
    func attachanimoter() {
        // 1.创建动画者对象
        self.animator = UIDynamicAnimator(referenceView: self.view)
        let p: CGPoint = CGPoint(x: 100, y: 200)
        // 2.创建行为
        self.snap = UISnapBehavior(item: self.button, snapToPoint: p)
        self.snap!.damping = 0.1; // 减幅
        
        // 3.把行为添加到动画者对象当中
        self.animator?.addBehavior(self.snap!)

    }
}

